<?php
	App::import('Model', 'Favorite');
	$f = new Favorite();
	if (!isset($open)) {
		$open = Configure::read('Delivery.is_open');
	}
	$id = ($open) ? 'ord-' . $product['Orderable']['id'] : 'pro-' . $product['Product']['product_id'];
	//this next boolean statement is a bit tough to swallow.  Here's how it works.
	// - Nobody can order before the Orderable table row exists for the product.  I.E. before the cycle starts.
	// - Only logged in people can order during the shopping cycle.
	// - Admins and Cashiers can order after the shopping cycle to adjust baskets.
	$canOrder = (($open || $current_user['auth_type'] == AUTH_CASH || $current_user['auth_type'] == AUTH_ADMIN) && ($current_user['auth_type'] != 'guest') && isset($product['Orderable']));
	$forceOrder = ($current_user['auth_type'] == AUTH_CASH || $current_user['auth_type'] == AUTH_ADMIN);
	echo "<div class='product' id='", $id, "'>\n";
	//$product['Subcategory'] = $cat['Subcategory'];
	if ($canOrder) {
		echo '<p class="price">$', number_format($product['Orderable']['price'],2), ' per ', $product['Orderable']['priced_by'];
	} else {
		echo '<p class="price">$', number_format($product['Product']['unit_price'],2), ' per ', $product['Product']['pricing_unit'], '<br/>', $html->link("More Info",'/products/info/'.$product['Product']['product_id']);
	}
	$imageOpts = array('width' => 80);
	if ($canOrder) {
		if ($product['Orderable']['inventory_start'] > 0) {
			if ($product['Orderable']['inventory_left'] <= 0 && !$forceOrder) {
				echo '<br/>', "\n", "<em>Sold Out!</em>";
			} else {
				echo '<br/>', "\n", $html->link("Order",'/orderables/order/'.$product['Orderable']['id']);
				$imageOpts['link'] = '/orderables/order/'.$product['Orderable']['id'];
			}
		} else {
			echo '<br/>', "\n", $html->link("Order",'/orderables/order/'.$product['Orderable']['id']);
			$imageOpts['link'] = '/orderables/order/'.$product['Orderable']['id'];
		}
	}
	echo '</p>', "\n";
	echo $image->getImage($product,$imageOpts);
	echo "<p>", $product['Product']['product_name'], "</p>\n";
	echo "<p>", $html->link($product['Member']['business_name'],array('controller' => 'producers', 'action' => 'about', $product['Member']['username'])), "</p>\n";
	$m = $current_user['member_id'];
	if ($m !== false) {
		echo "<p class='action' id='act-", $product['Product']['product_id'], "'>";
		if ($f->isFavorite($product['Product']['product_id'],$m)) {
			echo $ajax->link('Remove from Favorites','/favorites/delete/'.$product['Product']['product_id'], array('update'=>'act-' . $product['Product']['product_id']));
		} else {
			echo $ajax->link('Add to Favorites','/favorites/add/'.$product['Product']['product_id'], array('update'=>'act-' . $product['Product']['product_id']));
		}
		echo "</p>\n";
	}
	echo "</div>\n";
	
?>
